package com.chexiehui.jc.mvp.car;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.ShopInfo;
import com.chexiehui.jc.entity.TelInfo;

/**
 * @Description 车型详情
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class CarModelDetailsPresenter extends BasePresenter<CarModelDetailsCovenant.View, CarModelDetailsCovenant.Stores>
        implements CarModelDetailsCovenant.Presenter {

    public CarModelDetailsPresenter(CarModelDetailsCovenant.View view) {
        attachView(view);
    }

   /* @Override
    public void getSeriesInfo() {
        addSubscription(appStores.getSeriesInfo(mvpView.getSeriesId()),
                new ApiCallback<BaseModel<CarSeriesInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<CarSeriesInfo> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null) {
                                mvpView.onGetSeriesInfoSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "暂无车型信息");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetSeriesInfoFailure(new BaseModel<>(code, msg));
                    }
                });
    }*/
    @Override
    public void getSalespersonTel() {
        addSubscription(appStores.getSalespersonTel(mvpView.getSeriesId()),
                new ApiCallback<BaseModel<TelInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<TelInfo> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.onGetSalespersonTelSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetSalespersonTelFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void addInstallment(String carid, String shopids, String phone, String name, String city) {
        if(StringUtils.isTrimEmpty(carid)){
            mvpView.showToast("请选择车系");
            return;
        }
        if(StringUtils.isTrimEmpty(phone)){
            mvpView.showToast("请输入手机号");
            return;
        }
        if(!RegexUtils.isMobileSimple(phone)){
            mvpView.showToast("请输入正确手机号");
            return;
        }
        if(StringUtils.isTrimEmpty(name)){
            mvpView.showToast("请输入姓名");
            return;
        }
        if(StringUtils.isTrimEmpty(city)){
            mvpView.showToast("请选择城市");
            return;
        }
        if(StringUtils.isTrimEmpty(shopids)){
            mvpView.showToast("请选择经销商");
            return;
        }
        mvpView.showLoading("正在提交");
        addSubscription(appStores.addInstallment(carid, shopids, phone, name, city),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onAddInstallmentSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAddInstallmentFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void addShopInquiry(String carid, String shopids, String phone, String name, String city) {
        if(StringUtils.isTrimEmpty(carid)){
            mvpView.showToast("请选择车系");
            return;
        }
        if(StringUtils.isTrimEmpty(phone)){
            mvpView.showToast("请输入手机号");
            return;
        }
        if(!RegexUtils.isMobileSimple(phone)){
            mvpView.showToast("请输入正确手机号");
            return;
        }
        if(StringUtils.isTrimEmpty(name)){
            mvpView.showToast("请输入姓名");
            return;
        }
        if(StringUtils.isTrimEmpty(city)){
            mvpView.showToast("请选择城市");
            return;
        }
        if(StringUtils.isTrimEmpty(shopids)){
            mvpView.showToast("请选择经销商");
            return;
        }
        mvpView.showLoading("正在提交");
        addSubscription(appStores.addShopInquiry("1", carid, shopids, phone, name, city),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onAddShopInquirySuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAddShopInquiryFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getShopList(int pageNum) {
        addSubscription(appStores.getShopList(mvpView.getSeriesId(), mvpView.getLocationLat(),
                mvpView.getLocationLng(),
                mvpView.getDescType(), mvpView.getLocationCity(), 1, 10),
                new ApiCallback<BaseModel<BasePage<ShopInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<ShopInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null && model.getData().getItems().size() > 0) {
                                mvpView.onGetShopListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetShopListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}